<template>
  <ele-modal
    form
    :width="738"
    :model-value="modelValue"
    title="批量打印学生推荐表"
    @update:modelValue="updateModelValue"
  >
    <el-divider style="margin-top: 10px;margin-bottom: 10px;"></el-divider>
    <ele-loading :loading="loading" text="正在加载打印机" type="dot">
      <el-form :inline="true" :model="printerList" class="demo-form-inline">
        <el-form-item label="打印机">
          <el-select v-model="defaultPrinter" placeholder="请选择打印机" style="width: 350px;">
            <el-option v-for="(item,index) in printerList" :label="item" :value="item" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="goPrint">打印</el-button>
        </el-form-item>
      </el-form>
    </ele-loading>
    <el-divider style="margin-top: 0px;margin-bottom: 10px;"></el-divider>
    <div v-for="(studentObject,index) in studentObjectList" :key="index" class="table-container">
      <div class="print-container">
        <div ref="frontPage" class="frontPage">
          <div class="container-05">
            <h1>福建省普通高校毕业生<br/>就&nbsp;&nbsp;业&nbsp;&nbsp;推&nbsp;&nbsp;荐&nbsp;&nbsp;表
            </h1>
            <div class="line-header">
              <div class="line">
                <div class="title">姓名：</div>
                <div class="info">{{ studentObject.name }}</div>
              </div>
              <div class="line">
                <div class="title">学校：</div>
                <div class="info">福州大学至诚学院</div>
              </div>
              <div class="line">
                <div class="title">专业：</div>
                <div class="info">{{ studentObject.majorDirection }}</div>
              </div>
              <div class="line">
                <div class="title">学历：</div>
                <div class="info">本科</div>
              </div>
              <div class="line">
                <div class="title">毕业时间：</div>
                <div class="info">{{ studentObject.graduationYear }}-7-1</div>
              </div>
            </div>
          </div>
        </div>
        <div ref="backPage" class="backPage">
          <div class="container-03">
            <table>
              <tr>
                <td style="width: 60px; min-width: 60px;">姓名</td>
                <td style="width: 90px;">{{ studentObject.name }}</td>
                <td style="width: 60px; min-width: 60px;">性别</td>
                <td style="width: 70px;">{{ studentObject.sex }}</td>
                <td style="width: 60px; min-width: 60px;">政治<br/>面貌</td>
                <td style="width: 100px;">{{ studentObject.politicalStatus }}</td>
                <td style="width: 60px; min-width: 60px;">毕业<br/>时间</td>
                <td style="width: 80px;">{{ studentObject.graduationYear }}-7-1</td>
                <td rowspan="3">{{ studentObject.code }}</td>
              </tr>
              <tr>
                <td>专业</td>
                <td colspan="3">{{ studentObject.majorDirection }}</td>
                <td>学制</td>
                <td>{{ studentObject.studyTime }}年</td>
                <td>学历</td>
                <td>本科</td>
              </tr>
              <tr>
                <td>外语<br/>水平</td>
                <td colspan="3">{{ studentObject.foreignLanguageEvel }}</td>
                <td>计算机<br/>水平</td>
                <td colspan="3">{{ studentObject.computerLevel }}</td>
              </tr>
              <tr>
                <td>个人<br/>电话</td>
                <td colspan="2">{{ studentObject.phoneNumber }}</td>
                <td>QQ</td>
                <td colspan="2">{{ studentObject.qq }}</td>
                <td>电子<br/>邮箱</td>
                <td colspan="2">{{ studentObject.email }}</td>
              </tr>
              <tr>
                <td>职业资<br/>格证书</td>
                <td colspan="12" class="text-left" style="height: 65px; padding-left: 35px; padding-right: 5px;">
                  {{ studentObject.qualification }}
                </td>
              </tr>
              <tr>
                <td>家庭<br/>地址</td>
                <td colspan="12" class="text-left" style="height: 65px;padding-left: 35px; padding-right: 5px;">
                  {{ studentObject.homeAddress }}
                </td>
              </tr>
              <tr class="line-01">
                <td style="height: 125px;">获奖<br/>荣誉</td>
                <td class="cell-body body-01" colspan="12">
                  <div v-if="studentObject.rewardPunishmentPrize1 && studentObject.rewardPunishmentPrize1 != '无'">
                    {{ studentObject.rewardPunishmentPrize1 }}
                  </div>
                  <div v-if="studentObject.rewardPunishmentPrize2 && studentObject.rewardPunishmentPrize2 != '无'">
                    {{ studentObject.rewardPunishmentPrize2 }}
                  </div>
                  <div v-if="studentObject.rewardPunishmentPrize3 && studentObject.rewardPunishmentPrize3 != '无'">
                    {{ studentObject.rewardPunishmentPrize3 }}
                  </div>
                  <div v-if="studentObject.rewardPunishmentPrize4 && studentObject.rewardPunishmentPrize4 != '无'">
                    {{ studentObject.rewardPunishmentPrize4 }}
                  </div>
                  <div v-if="studentObject.rewardPunishmentPrize5 && studentObject.rewardPunishmentPrize5 != '无'">
                    {{ studentObject.rewardPunishmentPrize5 }}
                  </div>
                  <div
                    v-if="isEmptyString(studentObject.rewardPunishmentPrize1) && isEmptyString(studentObject.rewardPunishmentPrize2) && isEmptyString(studentObject.rewardPunishmentPrize3) && isEmptyString(studentObject.rewardPunishmentPrize4) && isEmptyString(studentObject.rewardPunishmentPrize5)">
                    无
                  </div>
                </td>
              </tr>
              <tr class="line-01">
                <td style="height: 125px;">实践<br/>经历</td>
                <td class="cell-body body-01" colspan="12">
                  <div v-if="studentObject.workContent1 && studentObject.workContent1 != '无'">
                    {{ studentObject.workContent1 }}
                  </div>
                  <div v-if="studentObject.workContent2 && studentObject.workContent2 != '无'">
                    {{ studentObject.workContent2 }}
                  </div>
                  <div v-if="studentObject.workContent3 && studentObject.workContent3 != '无'">
                    {{ studentObject.workContent3 }}
                  </div>
                  <div v-if="studentObject.workContent4 && studentObject.workContent4 != '无'">
                    {{ studentObject.workContent4 }}
                  </div>
                  <div v-if="studentObject.workContent5 && studentObject.workContent5 != '无'">
                    {{ studentObject.workContent5 }}
                  </div>
                  <div
                    v-if="isEmptyString(studentObject.workContent1) && isEmptyString(studentObject.workContent2) && isEmptyString(studentObject.workContent3) && isEmptyString(studentObject.workContent4) && isEmptyString(studentObject.workContent5)">
                    无
                  </div>
                </td>
              </tr>
              <tr class="line-02">
                <td>求职<br/>意向</td>
                <td class="cell-body body-01" colspan="12">{{ studentObject.jobIntention }}</td>
              </tr>
              <tr class="line-03">
                <td>系推荐<br/>意见</td>
                <td class="cell-body body-01" colspan="12">
                  {{ studentObject.departmentOpinion }}
                </td>
              </tr>
              <tr class="line-04">
                <td style="height: 120px;">学院<br/>推荐<br/>意见</td>
                <td class="fs-24" colspan="12">同意推荐就业！</td>
              </tr>
              <tr class="line-05">
                <td>学院<br/>联系人</td>
                <td class="cell-link" colspan="12">
                <span>{{ studentObject.counsellorName }}&nbsp;老师&nbsp;&nbsp;{{
                    studentObject.counsellorPhone
                  }}</span>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <span>学院毕业生就业指导中心电话&nbsp;&nbsp;0591-87893159</span>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </div>
    </div>
    <ele-modal :width="600" title="批量打印操作进行中" :model-value="showTipModal" :show-close="false" :modal="true"
               :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true">
      <el-alert
        :title="tipMessage"
        type="success"
        center
        :closable="false">
      </el-alert>
      <div v-show="printIsDone" slot="footer" class="dialog-footer">
        <el-button @click="showTipModal = false;">关闭</el-button>
      </div>
    </ele-modal>

  </ele-modal>
</template>


<script setup>
import {getLodop} from "@/assets/js/LodopFuncs";
import {ref, reactive, watch, onMounted} from 'vue';
import {EleMessage} from 'ele-admin-plus/es';
import {selectStudentRecommendInfos} from "@/api/manage/recommend/admin/index.js";


const printerList = ref([]);
const defaultPrinter = ref('');

const emit = defineEmits(['done', 'update:modelValue']);
const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  /**
   * 传入
   */
  data: Array
});

/** 提交状态 */
const loading = ref(false);


/**
 * 标识当前打印是否结束
 * @type {Ref<UnwrapRef<boolean>>}
 */
const printIsDone = ref(false);


const studentObjectList = ref([]);


const frontPage = ref(null);
const backPage = ref(null);

/** 更新modelValue */
const updateModelValue = (value) => {
  emit('update:modelValue', value);
};

const selectInfo = () => {
  // loading.value = true;
  let params = {code: props.data};
  selectStudentRecommendInfos(params).then(res => {
    // loading.value = false;
    console.log(res.data);
    studentObjectList.value = res.data;
  });
}

const initPrinterList = () => {
  let LODOP = getLodop()//调用getLodop获取LODOP对象
  //初始化打印机列表
  let printCount = LODOP.GET_PRINTER_COUNT();//获取打印机数量
  for (let i = 0; i < printCount; i++) {
    printerList.value.push(LODOP.GET_PRINTER_NAME(i));
  }
  defaultPrinter.value = LODOP.GET_PRINTER_NAME(-1);//获取默认打印机
}


const printStyle = ref(`
    .print-container * {
      padding: 0;
      margin: 0;
    }

    .print-container .pd-10 {
      padding: 10px;
    }

    .print-container .fs-24 {
      font-size: 24px;
    }

    .print-container .container-03 {
      width: 700px;
      height: 1000px;
      margin-left:auto;
      margin-right:auto;
      padding-top:40px;
      align-items: center;
    }

    .print-container .container-03 table {
      width: 700px;
      border-collapse: collapse;
    }

    .print-container .container-03 table td {
      height: 50px;
      border: solid 1px black;
      text-align: center;
    }

    .print-container .container-03 table .line-fr td {
      height: 1px;
      border: none;
    }

    .print-container .container-03 table .line-fr .cell-01 {
      width: 60px;
    }

    .print-container .container-03 table .line-fr .cell-02 {
      width: 46px;
    }

    .print-container .container-03 table .line-fr .cell-03 {
      width: 31px;
    }

    .print-container .container-03 table .line-fr .cell-04 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-05 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-06 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-07 {
      width: 30px;
    }

    .print-container .container-03 table .line-fr .cell-08 {
      width: 30px;
    }

    .print-container .container-03 table .line-fr .cell-09 {
      width: 40px;
    }

    .print-container .container-03 table .line-fr .cell-10 {
      width: 49px;
    }

    .print-container .container-03 table .line-fr .cell-11 {
      width: 40px;
    }

    .print-container .container-03 table .line-fr .cell-12 {
      width: 40px;
    }

    .print-container .container-03 table .line-fr .cell-13 {
      width: 100px;
    }

    .print-container .container-03 table .line-01 td {
      height: 107px;
    }

    .print-container .container-03 table .line-02 td {
      height: 60px;
    }

    .print-container .container-03 table .line-03 td {
      height: 145px;
    }

    .print-container .container-03 table .line-04 td {
      height: 60px;
    }

    .print-container .container-03 table .line-05 td {
      height: 60px;
    }

    .print-container .container-03 table .cell-title {
      padding: 0 30px;
    }

    .print-container .container-03 table .cell-body {
      text-align: left;
      vertical-align: top;
      text-indent: 2em;
      padding: 0 5px 0 0;
    }

    .print-container .container-03 table .cell-body.body-01 {
      vertical-align: middle;
    }

    .print-container .container-03 table .cell-link p:first-child {
      float: left;
      padding: 0 0 0 10px;
    }

    .print-container .container-03 table .text-left {
      text-align: left;
      padding: 0 30px;
    }

    .print-container .container-04 {
      width: 102px;
      height: 100%;
      padding: 890px 0;
      text-align: center;
    }

    .print-container .container-05 {
      width: 700px;
      height: 808px;
      margin-left:auto;
      margin-right:auto;
      text-align: center;
      padding-top: 230px;
    }

    .print-container .container-05 .line-header {
      margin-left: -50px;
    }
    .print-container .container-05 h1 {
      font-size: 49px;
      margin: 0px 0 355px 0;
      font-family:微软雅黑;
    }

    .print-container .container-05 .line {
      display: flex;
      justify-content: center;
      font-size: 20px;
      height: 45px;
      align-items: center;
    }

    .print-container .container-05 .line .title {
      font-weight: bold;
      width: 120px;
      text-align: right;
      font-family:微软雅黑;
    }

    .print-container .container-05 .line .info {
      border-bottom: solid black 1px;
      width: 290px;
    }


  `);


const eleMessage = ref(null);


const goPrint = () => {
  tipMessage.value = "正在初始化打印数据";
  eleMessage.value = EleMessage.warning({
    message: '请不要关闭或刷新当前页面，打印会持续进行',
    duration: 6000000,
    showClose: false
  });
  showTipModal.value = true;
  printIsDone.value = false;
  createPrintPage();//创建打印页面
}


const showTipModal = ref(false);

/**
 * 提示文字
 * @type {Ref<UnwrapRef<string>>}
 */
const tipMessage = ref("");


/**
 * 修改提示窗口文字
 */
const editTipMessage = (content, count, min, max) => {
  if (content != '') {
    tipMessage.value = content;
  } else {
    tipMessage.value = "当前任务数：" + count + ",正在打印:" + min + "-" + max + ",请不要刷新或者关闭当前窗口";
  }
}

/**
 * 根据任务总数获取延时的时间
 * @param _count
 * @returns {number}
 */
const getPrintDelayTime = (_count) => {
  let count = _count;//所要打印文件总数
  if (count < 50) {
    return 45000;
  } else if (count >= 50 && count <= 100) {
    return 55000;
  } else {
    return 65000;
  }
}


const printTimeout = (i, min, max) => {
  let count = studentObjectList.value.length; //打印推荐表总数
  let deleyTime = getPrintDelayTime(count);//获取下需要延时的时间，会根据份数的多少来做一些相应的调整
  let frontPageHtmls = frontPage.value;
  let backPageHtmls = backPage.value;
  let strStyleCSS = "<!DOCTYPE html><html><head><style type=\"text/css\">" + printStyle.value + "</style></head>";
  let htmlEnd = "</html>";
  LODOP.PRINT_INIT("打印学生推荐表任务" + (i + 1));//新建打印任务
  setTimeout(function () {
    for (let j = min; j <= max; j++) {
      let strHtml = "<div class='print-container'>" + frontPageHtmls[j].innerHTML + "</div>";
      LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");//设置纸张样式
      LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%", strStyleCSS + strHtml + htmlEnd);//设置打印内容
      LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "100%");//设置缩放比例
      LODOP.NewPage();
      strHtml = "<div class='print-container'>" + backPageHtmls[j].innerHTML + "</div>";
      LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");//设置纸张样式
      LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%", strStyleCSS + strHtml + htmlEnd);//设置打印内容
      LODOP.SET_SHOW_MODE("MESSAGE_PARSING_HTM", "正在加载要打印的内容，请稍等！");//设置消息显示模式
      LODOP.SET_PRINT_MODE("PRINT_DEFAULTSOURCE", 7);//设置纸盒 7-自动
      LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "100%");//设置缩放比例
      LODOP.SET_PRINTER_INDEXA(defaultPrinter.value);//设置打印机
      LODOP.SET_PRINT_MODE("PRINT_DUPLEX", 2);//长边双面打印
      LODOP.NewPage();
    }
    LODOP.SET_SHOW_MODE("MESSAGE_PARSING_HTM", "");
    LODOP.PRINT();
    editTipMessage('', count, min + 1, max + 1);
    if (max + 1 == count) {
      setTimeout(function () {
        editTipMessage('打印任务全部发送成功，请耐心等待打印结束，您可以关闭当前页面了！', 0, 0, 0);
        printIsDone.value = true;
        eleMessage.value.close();
      }, 3000);
    }
  }, i * deleyTime);
}

//创建页面打印任务
function createPrintPage() {
  let count = studentObjectList.value.length; //打印推荐表总数
  let frontPageHtmls = frontPage.value;
  let backPageHtmls = backPage.value;
  let strStyleCSS = "<!DOCTYPE html><html><head><style type=\"text/css\">" + printStyle.value + "</style></head>";
  let htmlEnd = "</html>";
  let time = Math.ceil((parseInt(count) / 5));//总共要循环的次数
  let min = 0;
  //如果打印的条数大于小于，那么直接看是打印，如果大于等于5，开启多任务打印
  let max = count < 5 ? count : 5;
  editTipMessage('', count, min + 1, max);
  LODOP.PRINT_INIT("批量打印学生推荐表任务1");
  //第一个任务先建立任务，后面的任务跑定时
  for (let j = min; j < max; j++) {
    let strHtml = "<div class='print-container'>" + frontPageHtmls[j].innerHTML + "</div>";
    LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");//设置纸张样式
    LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%", strStyleCSS + strHtml + htmlEnd);//设置打印内容
    LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "100%");//设置缩放比例
    LODOP.NewPage();
    strHtml = "<div class='print-container'>" + backPageHtmls[j].innerHTML + "</div>";

    LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");//设置纸张样式
    LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%", strStyleCSS + strHtml + htmlEnd);//设置打印内容

    LODOP.SET_SHOW_MODE("MESSAGE_PARSING_HTM", "正在加载要打印的内容，请稍等！");//设置消息显示模式
    LODOP.SET_PRINT_MODE("PRINT_DEFAULTSOURCE", 7);//设置纸盒 7-自动
    LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "100%");//设置缩放比例
    LODOP.SET_PRINTER_INDEXA(defaultPrinter.value);//设置打印机
    LODOP.SET_PRINT_MODE("PRINT_DUPLEX", 2);//长边双面打印
    LODOP.NewPage();
  }
  LODOP.SET_SHOW_MODE("MESSAGE_PARSING_HTM", "");
  LODOP.PRINT();
  if (count == max) {
    setTimeout(function () {
      editTipMessage('打印任务全部发送成功，请耐心等待打印结束，您可以关闭当前页面了！', null, null, null);
      printIsDone.value = true;
      eleMessage.value.close();
    }, 3000);
  }
  for (let i = 1; i <= time - 1; i++) {
    //计算打印最大值和打印最小值
    min = i * 5;
    max = min + 5 - 1;
    if (max >= count) {
      max = count - 1;
    }
    //每5份新建一个任务
    printTimeout(i, min, max);
  }
}


/**
 * 判断字符串是否为空
 * @param str
 * @returns {boolean}
 */
const isEmptyString = (str) => {
  if (str === null || str === undefined || str.trim() === '') {
    return true; // 字符串为空
  } else {
    return false; // 字符串不为空
  }
}


onMounted(() => {

  loading.value = true;
  selectInfo();
  //延迟1.5秒再执行
  setTimeout(function () {
    loading.value = false;
    initPrinterList();
  }, 1500); // 1500毫秒后执行

  //注册样式
  const style = document.createElement('style');
  style.innerHTML = printStyle.value;
  document.head.appendChild(style);
})


</script>

<style scoped>
.dialog-footer {
  text-align: center;
  margin-top: 15px;
}
</style>
